<?php
/**
 * Created by IntelliJ IDEA
 * Author: 张伯发
 * Date  : 2019/11/1
 * Time  : 17:07
 */

namespace App\Common\Utils;


class ConsulUtil
{
    /**
     * 获取服务列表
     * @param string $body
     * @param string $appName
     * @return array
     * @author 张伯发 2019/11/1 17:09
     */
    public static function getServerList(string $body, string $appName)
    {
        $serviceList = json_decode($body, true);
        if (empty($serviceList)) return [];

        $serviceList = DataUtil::groupHandle($serviceList, 'Service');

        $list = $serviceList[$appName];
        $services = [];
        foreach ($list as $item) {
            $rpcPort = $item['Meta']['RpcPort'];
            $host = $item['Address'];

            $services[] = "{$host}:{$rpcPort}";
        }
        return $services;
    }
}
